<?php

namespace app\api\controller;


use app\api\model\project\ProjectApply as ProjectApplyModel;
use app\common\controller\Api;
use app\api\model\project\ProjectOrder;
use app\common\model\User;

//服务的数据分析
class Projectdataanalyse extends Api
{
    protected $noNeedRight = ['*'];

    //订单收益汇总
    public function orderDataAnalyse()
    {
        $year = input('year','2025');
        $month_list = getMonthlyTimestamps($year);
        $data = [];
        $where = ['staff_user_id'=>$this->auth->id,'pay_status'=>1];
        foreach ($month_list as $k=>$v){
            //单量
            $order_count = ProjectOrder::where($where)
                ->where('createtime', 'between time', [$v['start_timestamp'], $v['end_timestamp']])
                ->count();
            //收益
            $order_money = ProjectOrder::where($where)
                ->where('createtime', 'between time', [$v['start_timestamp'], $v['end_timestamp']])
                ->sum('pay_price');
            $data[] = [
                'date'=>$v['month'],
                'order_count'=>$order_count,
                'order_money'=>$order_money
            ];
        }

        $this->success('',$data);
    }

    //服务项目收益汇总
    public function projectDataAnalyse()
    {
        $year = input('year','2025');
        $month_list = getMonthlyTimestamps($year);
        $data = [];

        foreach ($month_list as $k=>$v){
            //服务项目
            $project_list = ProjectApplyModel::alias('pa')
                ->join('project_staff ps','pa.staff_id = ps.id')
                ->where(['ps.user_id'=>$this->auth->id])
                ->where('pa.createtime', 'between time', [$v['start_timestamp'], $v['end_timestamp']])
                ->field('pa.id,pa.staff_id,pa.name,pa.status')
                ->order('ps.zan_num desc')
                ->select();
            foreach ($project_list as $kk=>$vv){
                //单量
                $order_count = ProjectOrder::where(['staff_user_id'=>$this->auth->id,'pay_status'=>1,'staff_id'=>$vv['staff_id']])
                    ->where('createtime', 'between time', [$v['start_timestamp'], $v['end_timestamp']])
                    ->count();
                $project_list[$kk]['order_count'] = $order_count;
                //收益
                $order_money = ProjectOrder::where(['staff_user_id'=>$this->auth->id,'pay_status'=>1,'staff_id'=>$vv['staff_id']])
                    ->where('createtime', 'between time', [$v['start_timestamp'], $v['end_timestamp']])
                    ->sum('pay_price');
                $project_list[$kk]['order_money'] = $order_money;
            }
            $data[] = [
                'date'=>$v['month'],
                'project_list'=>$project_list
            ];
        }
        $this->success('',$data);
    }

    //顾客会员等级汇总
    public function userDataAnalyse()
    {
        $year = input('year','2025');
        $date = getYearTimestamps($year);

        //用户列表
        $user_list = ProjectOrder::where(['staff_user_id'=>$this->auth->id,'pay_status'=>1])
            ->where('createtime', 'between time', [$date['start'], $date['end']])
            ->group('user_id')
            ->column('user_id');

        $data = [
            'F1星' => User::where(['level'=>1])->where('id','in',$user_list)->count(),
            'F2星' => User::where(['level'=>2])->where('id','in',$user_list)->count(),
            'F3星' => User::where(['level'=>3])->where('id','in',$user_list)->count(),
            'F4星' => User::where(['level'=>4])->where('id','in',$user_list)->count(),
            'F5星' => User::where(['level'=>5])->where('id','in',$user_list)->count(),
        ];

        $this->success('',$data);
    }

}